package com.example.controller;

import com.example.pojo.dto.EmployeeInsertDTO;
import com.example.pojo.dto.EmployeeLoginDTO;
import com.example.pojo.dto.EmployeePageDTO;
import com.example.pojo.dto.EmployeeUpdateDTO;
import com.example.pojo.entity.Employee;
import com.example.pojo.entity.result.PageResult;
import com.example.pojo.entity.result.Result;
import com.example.pojo.vo.EmployeeLoginVO;
import com.example.pojo.vo.EmployeePageVO;
import com.example.pojo.vo.EmployeeVO;
import com.example.properties.JwtProperties;
import com.example.service.EmployeeService;
import constant.JwtClaimsConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.example.utils.JwtUtil;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/emp")
@RequiredArgsConstructor
@Slf4j
@Api("职工相关接口")
public class EmployeeController {
    private final EmployeeService employeeService;
    private final JwtProperties jwtProperties;

    /**
     * 新增职工
     *
     * @param employeeInsertDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增职工")
    public Result insertEmp(@RequestBody EmployeeInsertDTO employeeInsertDTO) {
        log.info("新增职工：{}", employeeInsertDTO);
        employeeService.insertEmp(employeeInsertDTO);
        log.info("新增职工成功");
        return Result.success();
    }

    /**
     * 删除职工
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除职工")
    public Result deleteEmp(@RequestParam Long id) {
        log.info("删除职工：{}", id);
        employeeService.deleteEmp(id);
        log.info("删除职工成功");
        return Result.success();
    }

    /**
     * 修改员工信息
     *
     * @param employeeUpdateDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改员工信息")
    public Result updateEmp(@RequestBody EmployeeUpdateDTO employeeUpdateDTO) {
        log.info("修改员工信息：{}", employeeUpdateDTO);
        employeeService.updateEmp(employeeUpdateDTO);
        log.info("修改成功");
        return Result.success();
    }

    /**
     * 查询职工信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("查询职工信息")
    public Result<EmployeeVO> getEmpById(@PathVariable Long id) {
        log.info("查询职工信息：{}", id);
        EmployeeVO empVO = employeeService.getEmpById(id);
        log.info("查询成功");
        return Result.success(empVO);
    }

    /**
     * 分页查询职工列表
     *
     * @param employeePageDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("职工列表分页查询")
    public Result<PageResult<EmployeePageVO>> pageEmployee(EmployeePageDTO employeePageDTO) {
        log.info("分页查询职工列表：{}", employeePageDTO);
        PageResult<EmployeePageVO> pageResult = employeeService.pageEmployee(employeePageDTO);
        log.info("查询成功");
        return Result.success(pageResult);
    }

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation("员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .account(employee.getAccount())
                .name(employee.getName())
                .gender(employee.getGender())
                .token(token)
                .build();
        log.info("登陆成功");
        return Result.success(employeeLoginVO);
    }
}